package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.config.WikiApplication;
import com.jiawa.wiki.domain.Wcategory;
import com.jiawa.wiki.domain.WcategoryExample;
import com.jiawa.wiki.mapper.WcategoryMapper;
import com.jiawa.wiki.req.WcategoryQueryReq;
import com.jiawa.wiki.req.WcategorySaveReq;
import com.jiawa.wiki.resp.WcategoryQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.util.CopyUtil;
import com.jiawa.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WcategoryService {

    private static final Logger LOG = LoggerFactory.getLogger(WikiApplication.class);

    @Resource
    private WcategoryMapper wcategoryMapper;

    @Resource
    private SnowFlake snowFlake;
    /**
     * 查全部数据
     * @returnist
     */
    public List<WcategoryQueryResp> all() {
        WcategoryExample wcategoryExample = new WcategoryExample();
        // 排序
        wcategoryExample.setOrderByClause("sort asc");

        List<Wcategory> wcategoryList = wcategoryMapper.selectByExample(wcategoryExample);

        // 列表复制
        List<WcategoryQueryResp> list = CopyUtil.copyList(wcategoryList, WcategoryQueryResp.class);

        return list;
    }


    /**
     * 获取列表数据 带分页
     * @param req
     * @return
     */
    public PageResp<WcategoryQueryResp> list(WcategoryQueryReq req) {
        WcategoryExample wcategoryExample = new WcategoryExample();
        // 排序
        wcategoryExample.setOrderByClause("sort asc");
        // where条件
        WcategoryExample.Criteria criteria = wcategoryExample.createCriteria();
//        if(!ObjectUtils.isEmpty(req.getName())) {
//            criteria.andNameLike("%" + req.getName() + "%");
//        }
        // 分页设置
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Wcategory> wcategoryList = wcategoryMapper.selectByExample(wcategoryExample);

        PageInfo<Wcategory> pageInfo = new PageInfo<>(wcategoryList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

//        List<WcategoryResp> respList = new ArrayList<>();
//        for (Wcategory wcategory: wcategoryList
//             ) {
////            WcategoryResp wcategoryResp = new WcategoryResp();
////            BeanUtils.copyProperties(wcategory,wcategoryResp);
//            // 对象复制
//            WcategoryResp wcategoryResp = CopyUtil.copy(wcategory,WcategoryResp.class);
//            respList.add(wcategoryResp);
//        }
        // 列表复制
        List<WcategoryQueryResp> list = CopyUtil.copyList(wcategoryList, WcategoryQueryResp.class);
        // 封装返回的分页数据
        PageResp<WcategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 保存
     * @param req
     *
     */
    public void save(WcategorySaveReq req){
        Wcategory wcategory = CopyUtil.copy(req,Wcategory.class);
        if(ObjectUtils.isEmpty(req.getId())) {
            // 新增
            wcategory.setId(snowFlake.nextId());
            wcategoryMapper.insert(wcategory);
        }else{
            // 更新
            wcategoryMapper.updateByPrimaryKey(wcategory);
        }
    }

    /**
     * 根据主键id删除
     * @param id
     */
    public void delete(Long id) {
        LOG.info("主键：{}", id);
        wcategoryMapper.deleteByPrimaryKey(id);
    }
}
